package com.openstack4j.qx.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.openstack.OSFactory;

import com.openstack4j.qx.ResourceTemplateController;
import com.openstack4j.qx.imp.ResourceTemplateControllerImp;
import com.openstack4j.qx.pojo.BaseResp;
import com.openstack4j.qx.pojo.ResourceTemplate;

/**
 * 资源模板rest接口服务
 * 
 * @author Lynne
 *
 */
@Path("/ResourceTemplateRest")
public class ResourceTemplateRest {
	// 调用接口
	ResourceTemplateController controller = new ResourceTemplateControllerImp();

	/**
	 * 
	 * @param ResourceTemplateID
	 *            资源模板编号
	 * @param ResourceTemplate
	 *            模板xml
	 * @return 添加 查询用post 删除更改get 规范
	 */
	@POST
	@Path("/CreateResourceTemplateReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp CreateResourceTemplateReq(@Context HttpServletRequest request) {
		String ResourceTemplateID = request.getParameter("ResourceTemplateID");
		String ResourceTemplate = request.getParameter("ResourceTemplate");

		System.out.println("ResourceTemplateID=" + ResourceTemplateID);
		System.out.println("ResourceTemplate=" + ResourceTemplate);

		// 实例化 创资源模板返回 对象
		BaseResp baseResp = new BaseResp();
		try {
			baseResp = controller.createResourceTemplate(ResourceTemplateID, ResourceTemplate);
		} catch (Exception e) {
			e.printStackTrace();
			// 异常信息
			baseResp.setFaultstring(e.getMessage());
		}
		return baseResp;
	}

	/**
	 * 删除模板
	 * 
	 * @param ResourceTemplateID
	 * @return
	 */
	@GET
	@Path("/DeleteResourceTemplateReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp DeleteResourceTemplateReq(@Context HttpServletRequest request) {
		String ResourceTemplateID = request.getParameter("ResourceTemplateID");
		System.out.println("ResourceTemplateID=" + ResourceTemplateID);

		BaseResp baseResp = new BaseResp();
		try {
			baseResp = controller.DeleteResourceTemplate(ResourceTemplateID);
		} catch (Exception e) {
			e.printStackTrace();
			// 异常信息
			baseResp.setFaultstring(e.getMessage());
		}
		return baseResp;
	}

	/**
	 * 查询模板xml
	 * 
	 * @param ResourceTemplateID
	 * @return
	 */
	@GET
	@Path("/ViewResourceTemplateReqXML")
	@Produces(MediaType.APPLICATION_XML)
	public ResourceTemplate ViewResourceTemplateReqXML(@QueryParam("ResourceTemplateID") String ResourceTemplateID) {

		System.out.println("ResourceTemplateID=" + ResourceTemplateID);
		ResourceTemplate resp = new ResourceTemplate();
		try {
			// 业务代码编写
			// login and authentication֤
			OSClientV2 os = OSFactory.builderV2().endpoint("http://10.50.0.11:5000/v2.0")
					.credentials("admin", "ADMIN_PASS").tenantName("admin").authenticate();

			Flavor flavor = os.compute().flavors().get(ResourceTemplateID);

			resp.getId();

			// 赋值，返回值对象
			resp.setId(flavor.getId());
			resp.setName(flavor.getName());
			resp.setDisk(flavor.getDisk());
			resp.setRam(flavor.getRam());
			resp.setVcpus(flavor.getVcpus());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}
}
